layui.use(['laypage','jquery'],function(){
    var page = layui.laypage,$ = layui.$;
    var Commodity = {
        category:'',
        onClickSort:function(){
            $(this).addClass('active').siblings().removeClass('active');
        }
    };
    Commodity.onClickTree = function(e, treeId, treeNode){
        Commodity.initLayPage('',treeNode.id)
    };
    Commodity.initLayPage = function (mername,category) {
        if(!category){
            category = ''
        }
        if(!mername){
            mername = ''
        }
        var pageConf = {
            limit: 9,
            offset: 1,
            category:category,
            mername:mername
        };
        $.post("/mall/detail_list", pageConf, function (data) {
            page.render({
                elem: 'demo0',
                count: data.total,
                curr: pageConf.offset,
                limit: pageConf.limit,
                first: "首页",
                last: "尾页",
                layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                jump: function (obj, first) {
                    if (!first) {
                        pageConf.offset = (obj.curr-1)*9;
                        pageConf.limit = obj.limit;
                        $.post("/mall/detail_list", pageConf, function (data) {
                            Commodity.fillTable(data.rows);
                        });

                    }
                }
            });
            Commodity.fillTable(data.rows);
        });

    }

    Commodity.reinitIframe = function(){
        var J_iframe = self.parent.document.getElementsByClassName("J_iframe")[0];
        try{
            var bHeight = J_iframe.contentWindow.document.body.scrollHeight;
            var dHeight = J_iframe.contentWindow.document.documentElement.scrollHeight;
            var height = Math.max(bHeight, dHeight);
            J_iframe.height = height;
        }catch (ex){
            console.log(ex)
        }
    }
//填充表格数据
    Commodity.fillTable = function (data) {
        $("#list-cont").empty();
        $.each(data, function (index, obj) {
            var merStr = '';
            merStr+='<div class="item">\n' +
                '                  <div class="img">\n' +
                '                    <a href=\"/mall/details/'+obj.id+'\"><img src=\"/kaptcha/'+obj.picture+'\"></a>\n' +
                '                  </div>\n' +
                '                  <div class="text">\n' +
                '                    <p class="title">'+obj.mername+'</p>\n' +
                '                    <p class="price">\n' +
                '                      <span class="pri">￥'+obj.price+'</span>\n' +
                '                    </p>\n' +
                '                  </div>\n' +
                '                </div>'
            $("#list-cont").append(merStr);
        });
        Commodity.reinitIframe();
    }




    var ztree = new $ZTree("categoryTree", "/category/tree");
    ztree.bindOnClick(Commodity.onClickTree);
    ztree.init();
    Commodity.initLayPage(mername,Commodity.category);
    $('.sort a').on('click',Commodity.onClickSort)

});